package com.example.kingdee.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.example.kingdee.block.JuShuiTanBlock;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.config.RuoYiConfig;
import com.example.kingdee.dto.PurchaseSyncDto;
import com.example.kingdee.dto.SyncDto;
import com.example.kingdee.entity.*;
import com.example.kingdee.jstSync.JstCreateBillFactory;
import com.example.kingdee.jstSync.JstSyncAbstract;
import com.example.kingdee.jstSync.entity.JstResult;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import com.example.kingdee.util.JuShuiTanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class JuShuiTanService {
    @Autowired
    private TBxStMapper tBxStMapper;
    @Autowired
    private RuoYiConfig ruoYiConfig;
    @Autowired
    private SqlTool sqlTool;
    @Autowired
    private JuShuiTanBlock juShuiTanBlock;
    @Autowired
    private JstCreateBillFactory jstCreateBillFactory;

    /**
     * Function：获取AccessToken
     */
    public String getAccessToken(){
        String accessToken = null;
        try {
            accessToken = juShuiTanBlock.getAccessToken();
        }catch (Exception e){
            e.printStackTrace();
        }

        return accessToken;
    }


    //聚水潭采单据同步
    public String jstBillSync(SyncDto syncDto) {
        //获取实现类
        JstSyncAbstract jstSyncAbstract = jstCreateBillFactory.createObject(syncDto);
        //获取起止时间
        Map<String, String> startEndTime = sqlTool.getStartEndTime(syncDto.getStartTimeType(), syncDto.getEndTimeType(), syncDto.getStart(), syncDto.getEnd());
        //分割时间段
        List<CycleTimeVo> cycleList = sqlTool.getCycleList(7, startEndTime.get("starTime"), startEndTime.get("endTime"));
        for (CycleTimeVo cycleTimeVo : cycleList) {
            String startTimePara = cycleTimeVo.getStartTimeStr();
            String endTimeStrPara = cycleTimeVo.getEndTimeStr();
            PurchaseSyncDto purchaseSyncDtoPage = new PurchaseSyncDto(1, 50, startTimePara, endTimeStrPara, syncDto.getPoIds());
            purchaseSyncDtoPage.setStatusList(syncDto.getStatus());
            //获取总页数
            int pageCount = jstSyncAbstract.getOrderInfoPage(syncDto, purchaseSyncDtoPage);
            for (int i = 1; i <= pageCount; i++) {
                PurchaseSyncDto purchaseSyncDto = new PurchaseSyncDto(i, 50, startTimePara, endTimeStrPara, null);
                if (syncDto.getPoIds() != null){
                    //根据单号查询
                    purchaseSyncDto.setPoIds(syncDto.getPoIds());
                }
                //先从聚水潭上查询单据
                JstResult jstResult = jstSyncAbstract.selectJstResult(syncDto, purchaseSyncDto);
                //对单据进行处理
                jstSyncAbstract.kingdeeSync(syncDto, jstResult);
                try {
                    Thread.sleep(250); // 暂停250毫秒秒
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                Thread.sleep(3 * 1000); // 暂停3秒
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return "200";
    }

    //聚水潭仓库同步
    public void jstStockSync() {
        String stock = juShuiTanBlock.getStock();
        //格式化单据
        List<JstStock> jstPurchases = JSONArray.parseArray(stock, JstStock.class);
        for (JstStock jstPurchase : jstPurchases) {
            String orgName = tBxStMapper.selectJstStockName(jstPurchase.getWmsCoId());
            if (StringUtils.isEmpty(orgName)){
                tBxStMapper.insertJstStock(jstPurchase);
            }else {
                tBxStMapper.updateJstStock(jstPurchase);
            }

        }
    }

    //聚水潭物流公司同步
    public void jstEmpSync() {

        String emp = juShuiTanBlock.getEmp();
        //格式化单据
        List<JstEmp> jstEmps = JSONArray.parseArray(emp, JstEmp.class);
    }

    //云星空单据上传
    public String kindeeBillSync(SyncDto syncDto) {
        //获取实现类
        JstSyncAbstract jstSyncAbstract = jstCreateBillFactory.createObject(syncDto);
        //获取要上传的数据
        JstResult jstResult = jstSyncAbstract.selectJstResult(syncDto, new PurchaseSyncDto());
        //上传数据到聚水潭
        jstSyncAbstract.kingdeeSync(syncDto, jstResult);
        return "200";
    }

    //聚水潭采购订单同步
//    public String purchaseSync(SyncDto syncDto) {
//        JstSyncAbstract jstSyncAbstract = jstCreateBillFactory.createObject(syncDto);
//        //获取起止时间
//        Map<String, String> startEndTime = new HashMap<>();
//        if (syncDto == null){
//            //默认查询七天内的数据
//            startEndTime = sqlTool.getStartEndTime(Calendar.DATE, Calendar.DATE, -6, 1);
//        }else {
//            //根据参数进行查询
//            startEndTime = sqlTool.getStartEndTime(syncDto.getStartTimeType(), syncDto.getEndTimeType(), syncDto.getStart(), syncDto.getEnd());
//        }
//        //分割时间段
//        List<CycleTimeVo> cycleList = sqlTool.getCycleList(7, startEndTime.get("starTime"), startEndTime.get("endTime"));
//        for (CycleTimeVo cycleTimeVo : cycleList) {
//            String startTimePara = cycleTimeVo.getStartTimeStr();
//            String endTimeStrPara = cycleTimeVo.getEndTimeStr();
//            PurchaseSyncDto purchaseSyncDtoPage = new PurchaseSyncDto(1, 50, startTimePara, endTimeStrPara, null);
//            //获取总页数
//            int pageCount = JuShuiTanUtil.getOrderInfoPage(purchaseSyncDtoPage, FormIdEnum.purchaseQuery, juShuiTanBlock.getAccessToken());
//            for (int i = 1; i <= pageCount; i++) {
//                PurchaseSyncDto purchaseSyncDto = new PurchaseSyncDto(i, 50, startTimePara, endTimeStrPara, null);
//                if (syncDto != null){
//                    //根据单号查询
//                    purchaseSyncDto.setPoIds(syncDto.getPoIds());
//                }
//                //先从聚水潭上查询采购单
//                List<JstPurchase> jstPurchaseList = juShuiTanBlock.getPurchaseInfo(purchaseSyncDto);
//                if (jstPurchaseList != null && jstPurchaseList.size() != 0){
//                    juShuiTanBlock.savaPurchase(jstPurchaseList, "");
//                }
//                try {
//                    Thread.sleep(250); // 暂停250毫秒秒
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            try {
//                Thread.sleep(3 * 1000); // 暂停3秒
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        return "200";
//    }
}
























